package com.icangya.DAO.impl;

import com.icangya.DAO.IRwGoodOrderDAO;
import com.icangya.Utils.SQLUtilt;
import com.icangya.domain.RwGoodOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import static com.icangya.Value.FootBall.FOOT_PAGE_ROW;

@Repository
public class RwGoodOrderDAOImpl extends  BaseDAO<RwGoodOrder> implements IRwGoodOrderDAO {


    Logger logger = LoggerFactory.getLogger(this.getClass());


    @Override
    public List<RwGoodOrder> getOrdersByUserId(long userid) {

        String hql=" FROM RwGoodOrder rworder where rworder.userid = ? ";

        List<RwGoodOrder> list=(List<RwGoodOrder>) getHibernateTemplate().find(hql,userid);

        if(list.size()>0){
            return list;
        }

        return null;
    }

    @Override
    public List<RwGoodOrder> getRwGoodOrderByOrderId(String orderId) {
        String hql=" FROM RwGoodOrder rworder where rworder.orderid = ? ";

        List<RwGoodOrder> list=(List<RwGoodOrder>) getHibernateTemplate().find(hql,orderId);

        if(list.size()>0){
            return list;
        }

        return null;
    }

    @Override
    public List<RwGoodOrder> getOrderListByPayStataAndSendStatus(int payStatus, int sendStatus) {

        String hql=" FROM RwGoodOrder rworder where rworder.paystatus = ? and sendStatus = ? ";

        List<RwGoodOrder> list=(List<RwGoodOrder>) getHibernateTemplate().find(hql,payStatus,sendStatus);

        if(list.size()>0){
            return list;
        }

        return null;
    }

    @Override
    public List<Object> findOrderByUserId(Long uid, Integer pageNum) {
        String hql="FROM RwGoodOrder er where er.userid='"+uid+"' order by er.createTime desc";
        String count = "SELECT COUNT(er) " + hql;
        Long total = (Long) getHibernateTemplate().find(count).get(0);
        return SQLUtilt.getIstance().doPage(getHibernateTemplate(), hql, total, pageNum, FOOT_PAGE_ROW);
    }

    @Override
    public Long getUserOrderCountByNow(long uid) {
        String hql="SELECT COUNT(go) FROM  RwGoodOrder go where  TO_DAYS(go.createTime)=TO_DAYS(?) and  go.userid="+uid ;

        return (Long) getHibernateTemplate().find(hql,new Timestamp(new Date().getTime())).get(0);
    }
}
